package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.CollectionQueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private CommentMapper commentMapper;//用于查询评论数

    @Override
    public PageInfo<Travel> query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,null);
        List<Travel> list = travelMapper.queryForList(qo);
        //查询收藏和点赞数遍历给list的对象设置值
        for (Travel travel : list) {
            travel.setCollectionNum(travelMapper.selectCollectionNumByTravelId(travel.getId()));
            travel.setLikeNum(travelMapper.queryLikeCountByTravelId(travel.getId()));

        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Travel> queryReasonable(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryForList(qo);
        for (Travel travel : list) {
            int commentNum = commentMapper.selectForNum(travel.getId(), Comment.MODELSTATE_TRAVEL);
            travel.setCommentNum(commentNum);
        }
        return new PageInfo<>(list);
    }

    @Override
    public void updateState(Travel travel) {
        travelMapper.updateState(travel);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        travel.setLastUpdateTime(new Date());
        if (travel.getId()!=null){
            //更新游记基本信息
            travelMapper.updateByPrimaryKey(travel);
            //拿到基本信息的id
            TravelContent travelContent = travel.getTravelContent();
            //主键关联,设置游记文本的id
            travelContent.setId(travel.getId());
            //保存游记文本内容
            travelContentMapper.updateByPrimaryKey(travelContent);
        }else {
            //设置作者
            travel.setAuthor(UserContext.getCurrentUser());
            //保存游记基本信息
            travelMapper.insert(travel);
            //拿到基本信息的id
            TravelContent travelContent = travel.getTravelContent();
            //主键关联,设置游记文本的id
            travelContent.setId(travel.getId());
            //保存游记文本内容
            travelContentMapper.insert(travelContent);
        }
    }

    @Override
    public Travel selectTravelById(Long id) {
        //查询游记信息
        Travel travel = travelMapper.selectByPrimaryKey(id);
        //查询当前用户的有游记数量
        User author = travel.getAuthor();
        author.setTravelNum(travelMapper.CountForTravel(author.getId()));
        //评论数回显
        travel.setCommentNum(commentMapper.selectForNum(id, Comment.MODELSTATE_TRAVEL));
        //查询游记内容
        TravelContent content = travelContentMapper.selectByPrimaryKey(id);
        //设置游记内容
        travel.setTravelContent(content);
        //查询点赞数量
        int likeNum = travelMapper.queryLikeCountByTravelId(id);
        //设置点赞数量
        travel.setLikeNum(likeNum);
        if (UserContext.getCurrentUser()!=null){
            //查询当前用户是否已经点赞
            //获取当前用户的id
            Long uid = UserContext.getCurrentUser().getId();
            //查询当前用户是否已点赞
            int isLike = travelMapper.queryIsLike(uid, id);
            //处理查询结果
            if (isLike>0){
                travel.setLike(true);
            }else {
                travel.setLike(false);
            }
        }
        return travel;
    }

    @Override
    public TravelContent selectTravelContentById(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo query(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,null);
        List<Travel> list = travelMapper.query(qo);
        return new PageInfo<>(list);
    }

    @Override
    public int queryForCount(SearchQueryObject qo) {
        return travelMapper.queryForCount(qo);
    }

    @Override
    public void updateBrowseNumById(Long id) {
        travelMapper.updateBrowseNumById(id);
    }
}
